package com.zs.zcerp.approval.controller;

import com.zs.zcerp.approval.entity.QhBasestdMEntity;
import com.zs.zcerp.approval.entity.QhBillstdD;
import com.zs.zcerp.approval.service.IQhBasestdDService;
import com.zs.zcerp.approval.service.IQhBasestdMService;
import com.zs.zcerp.approval.service.IQhbillstddService;
import com.zs.zcerp.common.annotation.ControllerEndpoint;
import com.zs.zcerp.common.controller.BaseController;
import com.zs.zcerp.common.entity.FebsResponse;
import com.zs.zcerp.common.entity.QueryRequest;
import com.zs.zcerp.common.entity.Strings;
import com.zs.zcerp.common.exception.FebsException;
import com.zs.zcerp.common.utils.FebsUtil;
import com.zs.zcerp.entrance.entity.Syorg;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;


/**
 * 基本流程设置
 *
 * @author Henry
 * @email *****@mail.com
 * @date 2021-04-09 20:58:44
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("baseflow")
public class QhBasestdMController extends BaseController {

    private final IQhBasestdMService iQhBasestdMService;
    private final IQhbillstddService iQhbillstddService;
    private final IQhBasestdDService iQhBasestdDService;



    @GetMapping("list")
    @RequiresPermissions("baseflow:view")
    public FebsResponse baseList(QhBasestdMEntity qhBasestdMEntity, QueryRequest request) {
        if(qhBasestdMEntity.getBasestdNo()!=null){
            qhBasestdMEntity.setBasestdNo( qhBasestdMEntity.getBasestdNo().trim());
        }
        if(qhBasestdMEntity.getNameT()!=null){
            qhBasestdMEntity.setNameT(qhBasestdMEntity.getNameT().trim());
        }
        return new FebsResponse().success()
                    .data(getDataTable(iQhBasestdMService.findBaseDetailList(qhBasestdMEntity, request)));
    }

    @PostMapping("add")
    @RequiresPermissions("baseflow:add")
    @ControllerEndpoint(operation = "新增流程", exceptionMessage = "新增流程失败")
    //@ResponseBody
    public FebsResponse addBase(QhBasestdMEntity qhBasestdmEntity) {
        if(  qhBasestdmEntity.getBillstdNo() ==null){
            return new FebsResponse().data("数据为空");
        }

        qhBasestdmEntity.setNameT(qhBasestdmEntity.getNameT().trim());
        //自动生成流程ID
       String basestdNo = FebsUtil.get32UUID();
        qhBasestdmEntity.setBasestdNo(basestdNo);

        String username   = FebsUtil.getCurrentUser().getUsername();
        qhBasestdmEntity.setLastUser(username);
        iQhBasestdMService.createBase(qhBasestdmEntity);

        QhBillstdD qhBillstdD = new QhBillstdD();
        qhBillstdD.setOrgId(qhBasestdmEntity.getOrgId());
        qhBillstdD.setBillstdNo(qhBasestdmEntity.getBillstdNo());
        qhBillstdD.setBillstdG(qhBasestdmEntity.getBillstdG());
        qhBillstdD.setBillstdgNm(qhBasestdmEntity.getDeptName());
        qhBillstdD.setBasestdNo(basestdNo);
        qhBillstdD.setAdmUser(username);
        qhBillstdD.setGrtUser(username);
        qhBillstdD.setLastDate(new Date());
        qhBillstdD.setLastUser(username);
        qhBillstdD.setGrtDept("ERP");
        iQhbillstddService.createBase(qhBillstdD);
        return new FebsResponse().success();
    }


    /***
     * 获取分类子表
     * @return
     */
    @GetMapping("checkBill")
    @ControllerEndpoint(operation = "新增流程", exceptionMessage = "类型检查唯一")
    public FebsResponse checkBill(QhBasestdMEntity qhBasestdmEntity)  {

        if(qhBasestdmEntity.getOrgId()==null&& qhBasestdmEntity.getBillstdNo() ==null){
            return new FebsResponse().success()
                    .data("查询数据为空");
        }

        List<QhBillstdD> qhBillstdDList = iQhbillstddService.findBillstdList(qhBasestdmEntity);
        Integer flag = 0;
        // 为0表示存在重复
        if(qhBillstdDList!=null&&!qhBillstdDList.isEmpty()){
             flag = 0;
        }else {
            flag = 1;
        }
        return new FebsResponse().success().data(flag);
    }

    @GetMapping("getEmpNO")
    @ControllerEndpoint(operation = "获取全部用户信息", exceptionMessage = "获取用户机构部门")
    public FebsResponse getEmpNoAndDeptNameList(String name)  {

        List<QhBillstdD> qhBillstdDList = iQhbillstddService.findEmpNoAndDeptNameList(name);
        return new FebsResponse().success().data(qhBillstdDList);
    }

    /***
     * 获取分类子表
     * @return
     */
    @GetMapping("getBillList")
    @ControllerEndpoint(operation = "单据分类", exceptionMessage = "获取单据分类失败")
    public FebsResponse getBillList( @Valid QhBasestdMEntity qhBasestdmEntity)  {
        List<QhBillstdD> qhBillstdDList = iQhbillstddService.findBillstdGList(qhBasestdmEntity);
        return new FebsResponse().success()
                .data(qhBillstdDList);
    }



    @PostMapping("update")
    @RequiresPermissions("baseflow:update")
    @ControllerEndpoint(operation = "修改流程", exceptionMessage = "修改流程失败")
    public FebsResponse updateBase(@Valid QhBasestdMEntity qhBasestdMEntity) {
        if (qhBasestdMEntity.getBasestdNo() == null) {
            throw new FebsException("流程编码不能空");
        }
        qhBasestdMEntity.setLastUser(FebsUtil.getCurrentUser().getUsername());
            iQhBasestdMService.updateBase(qhBasestdMEntity);
        return new FebsResponse().success();
    }


    @GetMapping("delete/{basestdNos}")
    @RequiresPermissions("baseflow:delete")
    @ControllerEndpoint(operation = "删除流程", exceptionMessage = "删除流程失败")
    public FebsResponse deleteBase(@NotBlank(message = "{required}") @PathVariable String basestdNos) {
        iQhBasestdMService.deleteBaseByBasestdNo(StringUtils.split(basestdNos, Strings.COMMA));
        //iQhbillstddService.deleteBybillstdG(StringUtils.split(basestdNos, Strings.COMMA));
        iQhBasestdDService.deleteBasestdMByBasestdNo(StringUtils.split(basestdNos, Strings.COMMA));
        iQhbillstddService.deleteBillstddByBasestdNo(StringUtils.split(basestdNos, Strings.COMMA));
        return new FebsResponse().success();
    }


    @GetMapping("check/{basestdNo}")
    public boolean checkBasestdNo(@NotBlank(message = "{required}") @PathVariable String basestdNo) {
        return iQhBasestdMService.findByBasestdNo(basestdNo) == null || StringUtils.isNotBlank(basestdNo);
    }

    @GetMapping("{basestdNo}")
    public QhBasestdMEntity getBase(@NotBlank(message = "{required}") @PathVariable String basestdNo) {
        return iQhBasestdMService.findBaseDetailList(basestdNo);
    }

    /**
     * 单据分类  查询
     * @return
     */
    @GetMapping("getList")
    public FebsResponse getlist() {
        return new FebsResponse().success().data(iQhBasestdMService.findList());

    }

    /**
     * 获取erp 分类
     * @param orgId
     * @return
     */
    @GetMapping("getNames")
    @RequiresPermissions("baseflow:view")
    public FebsResponse  getNames(String orgId) {
        return new FebsResponse().success().data(iQhBasestdMService.findByorgId(orgId));
    }


}
